<?php

namespace App\Http\Controllers;

use DB;
use Illuminate\Http\Request;
use Validator;

class UserController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    public function index(){

    }

    public function show($id){

    }

    public function store(Request $request){
        $input = $request->all();
        //参数检验
        $validator = Validator::make($input,
            [
                'name'      => 'required|string|min:2',
                'icon'      => 'required|string|min:2',
            ]
        );
        if($validator->fails()){
            return $this::jsonResponse(true,$validator->messages(),"paramError");
        }

        //处理
        $name     = $input["name"];
        $icon     = $input["icon"];

        $userData = [
            "name" => $name,
            "icon" => $icon,
            "mlimit_count" => 200
        ];

        $userId = DB::table("im_user")->insertGetId($userData);

        return $this::jsonResponse(false,$userId);
    }

    public function update(Request $request, $id){
        $input = $request->all();
        //参数检验
        $validator = Validator::make($input,
            [
                'name'      => 'string|min:2',
                'icon'      => 'string|min:2',
            ]
        );
        if($validator->fails()){
            return $this::jsonResponse(true,$validator->messages(),"paramError");
        }

        //处理
        $userData = [];
        foreach (["name","icon"] as $value) {
            if(!empty($input[$value])) $userData[$value] = $input[$value];
        }
        
        if(empty($userData))
            return $this::jsonResponse(true,"","UpdateDataEmpty");

        $userUpdateResult = DB::table("im_user")->where("id",$id)->update($userData);

        return $this::jsonResponse(false,$userUpdateResult);
    }

    public function destroy($id){

    }

}
